package view;

import java.awt.Color;
import main.WinkelApplication;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.net.URL;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingConstants;
import model.Product;

public class ProductList extends JPanel implements MouseListener, ActionListener {
    private final Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();     
    private ClassLoader cldr = this.getClass().getClassLoader();
    
    
    private final int categoryId;
    private java.net.URL dot   = cldr.getResource("view/dot.png");
    private URL BackButton = cldr.getResource("view/terug.png");
    
    public ProductList(int categoryId) {
        super();
       
        this.setLayout(null);
        this.categoryId = categoryId;
        initComponents();
    }

    private void initComponents() {
        addTitle();
        addProductItems();
        addBasket();
        addBack();
    }

    private void addProductItems() {
        List<Product> products = WinkelApplication.getQueryManager().getProducts(categoryId);
        add(buildPanelWithProducts(products, 20, (dim.height/12)+20));
    }
    
    private void addBack() {                      
        ImageIcon icon = new ImageIcon(BackButton);
        JLabel back = new JLabel(icon);
        back.setName("-1");
        back.setBounds(dim.width -((dim.width/5) + 82), 40, 48, 48);
        back.addMouseListener(this);
        add(back);
    }
    
    // Maakt de scrollpane met daarin alle producten en de bijhorende informatie
    private JScrollPane buildPanelWithProducts(List<Product> product, int x, int y){
        JPanel panel = new JPanel();
        panel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
        panel.setPreferredSize(new Dimension(dim.width-((dim.width/5)+80), product.size() * (dim.height/12)));
        
        JScrollPane scrlP = new JScrollPane(panel);
        scrlP.setSize(dim.width-((dim.width/5)+60), dim.height-(dim.height/6));
        scrlP.setBorder(null);
        scrlP.setLocation(x, y);
        scrlP.getViewport().add(panel);
        
        for(int i = 0; i < product.size(); i++){
            if(product.get(i).getStatus() == 1)
            panel.add(getProductItemPanel(product.get(i)));
        }
        
        return scrlP;
    }
    
    // Maakt een panel voor een product
    private JPanel getProductItemPanel(Product product) {
        JPanel productPanel = new JPanel();
        productPanel.setPreferredSize(new Dimension(dim.width-((dim.width/5)+80), dim.height/12));
        productPanel.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.BOTH;
        
        ImageIcon icon = new ImageIcon(dot);
        JLabel lblDot = new JLabel(icon);
        c.gridx = 0;
        c.gridy = 0;
        c.weighty = 0.7;
        productPanel.add(lblDot, c);

        JLabel lblProduct = new JLabel(product.getName());
        lblProduct.setFont(WinkelApplication.computeFontSize(2));
        lblProduct.addMouseListener(this);
        lblProduct.setName(String.valueOf(product.getProductId()));
        lblProduct.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        c.gridx = 1;
        c.gridy = 0;
        c.insets = new Insets(0, 5, 0, 0);
        c.weightx = 1;
        productPanel.add(lblProduct, c);

        JLabel lblDescription = new JLabel(product.getDescription());
        lblDescription.setFont(WinkelApplication.computeFontSize(3));
        c.gridx = 1;
        c.gridy = 1;
        c.weightx = 0; // Default
        c.weighty = 0.3;
        productPanel.add(lblDescription, c);
        
        JLabel lblVoorraad = new JLabel("Voorraad: " + product.getVoorraad());
        lblVoorraad.setFont(WinkelApplication.computeFontSize(3));
        c.gridx = 3;
        c.gridy = 0;
        c.weightx = 0; // Default
        c.weighty = 1;
        productPanel.add(lblVoorraad, c);

        JLabel lblPrice = new JLabel(WinkelApplication.CURRENCY + 
                WinkelApplication.PRICE_FORMAT.format(product.getPrice()));
        lblPrice.setFont(WinkelApplication.computeFontSize(2));
        c.gridx = 2;
        c.gridy = 1;
        c.insets = new Insets(0, 0, 0, 50);
        productPanel.add(lblPrice, c);

        JButton btnOrder = new JButton("Bestel");
        btnOrder.setFont(WinkelApplication.computeFontSize(2));
        btnOrder.setName(String.valueOf(product.getProductId()));
        btnOrder.addActionListener(this);
                
        if(product.getVoorraad()<=0){
            btnOrder.setEnabled(false);
            lblPrice.setForeground(Color.red);
            lblDescription.setForeground(Color.red);
            lblVoorraad.setForeground(Color.red);
        }
        
        c.gridx = 3;
        c.gridy = 1;
        c.insets = new Insets(0, 0, 0, 0); // Default
        c.ipadx = 25;
        productPanel.add(btnOrder, c);
        
        return productPanel;
    }

    private void addBasket() {
        BasketDisplay basket = new BasketDisplay();
        add(basket);
    }

    private void addTitle() {   
        JLabel lblTitle1 = new JLabel("Winkelapplicatie - Producten in " + 
                WinkelApplication.getQueryManager().getCategoryName(categoryId),SwingConstants.CENTER);
        lblTitle1.setBounds(20, 20, (dim.width - ((dim.width/5)+40)), dim.height/12);        
        lblTitle1.setFont(WinkelApplication.computeFontSize(1));
        lblTitle1.setName("-1");
        add(lblTitle1);
    }

    @Override
    public void mouseClicked(MouseEvent event) {
        JLabel label = (JLabel) event.getSource();
        if (label.getName().equals("-1")) {
            // the user clicked on the title, go back to the first screen
            WinkelApplication.getInstance().showPanel(new view.CategoryList());
        } else {
            WinkelApplication.getInstance().showPanel(new view.ProductDetails(Integer.parseInt(label.getName())));
        }
    }

    @Override
    public void mouseEntered(MouseEvent event) {
        JLabel label = (JLabel) event.getSource();
        label.setBackground(new Color(151, 186, 225));
        label.setForeground(Color.blue);
    }

    @Override
    public void mousePressed(MouseEvent event) {
        // Intentionally left blank.
    }

    @Override
    public void mouseReleased(MouseEvent event) {
        // Intentionally left blank.
    }

    @Override
    public void mouseExited(MouseEvent event) {
        JLabel label = (JLabel) event.getSource();
        label.setBackground(new Color(151, 186, 225));
        label.setForeground(Color.black);
    }

    @Override
    public void actionPerformed(ActionEvent event) {
        String product_id = ((JButton) event.getSource()).getName();
        Product product = WinkelApplication.getQueryManager().getProduct(Integer.parseInt(product_id));
        WinkelApplication.getBasket().addProduct(product);
    }

    @Override
    public void paint(Graphics graphics) {
        super.paint(graphics);
        //graphics.drawLine(20, 45, 540, 45);
    }
}
